package com.wanmait.employment.webController;

import com.wanmait.employment.entity.*;
import com.wanmait.employment.mapper.UserinfoMapper;
import com.wanmait.employment.service.UserinfoService;
import com.wanmait.employment.util.AjaxResult;
import com.wanmait.employment.util.JWTUtils;
import org.apache.catalina.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Value;
import org.apache.shiro.crypto.hash.SimpleHash;

@RestController
@RequestMapping("web/user")
public class WebUserinfoController {
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    private UserinfoMapper userinfoMapper;
    @Value("${hash.algorithmName}")
    private String algorithmName;
    @Value("${hash.iterations}")
    private Integer iterations;



    @GetMapping("login")
    public AjaxResult login(String tel,String userpass){
        Userinfo userinfo=new Userinfo();
        userinfo.setTel(tel);
        userinfo.setUserpass(userpass);
        //查询登录用户的盐
        Userinfo userinfos= userinfoMapper.selecttel(userinfo.getTel());
        //加密
        SimpleHash simpleHash = new SimpleHash(algorithmName,userinfo.getUserpass(),userinfos.getSalt(),iterations);
        userinfo.setUserpass(simpleHash.toString());
        Userinfo userinfo1 = userinfoService.login(userinfo);
        if(userinfo1==null){
            return AjaxResult.error(10001,"登录失败了");
        }
        //生成token
        Map<String,String> claims = new HashMap<>();
        claims.put("username",userinfo.getUsername());
        claims.put("name",userinfo.getName());
        String token  = JWTUtils.createToken(userinfo.getId()+"",60,userinfo.getUserpass(),claims);

        //使用token作为键，把admin用户对象放到redis中，设置过期时间和token的过期时间一致
        redisTemplate.opsForValue().set("token:"+token,userinfo1,60, TimeUnit.MINUTES);

        userinfo1.setUserpass(null);
        return AjaxResult.success("登录成功").put("token",token).put("userinfo",userinfo1);
    }


    @GetMapping("register")
    public AjaxResult register(String tel,String password){
        Userinfo userinfo=new Userinfo();
        userinfo.setUserpass(password);
        userinfo.setTel(tel);
//加盐
        String salt = UUID.randomUUID().toString().replaceAll("-","");
        userinfo.setSalt(salt);
        //对密码进行加密
        SimpleHash simpleHash = new SimpleHash(algorithmName,userinfo.getUserpass(),salt,iterations);
        userinfo.setUserpass(simpleHash.toString());
        userinfoService.register(userinfo);
        Userinfo userinfo1 = userinfoService.login(userinfo);
        if(userinfo1==null){
            return AjaxResult.error(10001,"登录失败了");
        }
        //生成token
        Map<String,String> claims = new HashMap<>();
        claims.put("username",userinfo.getUsername());
        claims.put("name",userinfo.getName());
        String token  = JWTUtils.createToken(userinfo.getId()+"",60,userinfo.getUserpass(),claims);

        //使用token作为键，把admin用户对象放到redis中，设置过期时间和token的过期时间一致
        redisTemplate.opsForValue().set("token:"+token,userinfo1,60, TimeUnit.MINUTES);

        userinfo1.setUserpass(null);
        return AjaxResult.success("注册成功").put("token",token).put("userinfo",userinfo1);
    }

       @GetMapping ("listAll")
        public AjaxResult listAll(){
           List<RecruitmentEducation> list=userinfoService.relistAll();
           return AjaxResult.success("查询成功").put("relist",list);
        }
        @GetMapping("welist")
        public AjaxResult welist(){
        List<WorkExperience>list=userinfoService.welistAll();
        return AjaxResult.success("查询成功").put("welist",list);
        }
        @PostMapping("userdate")
        public  AjaxResult userdate(@RequestBody Userinfo user){
           user.setRecruitmentId(user.getRecruitmentEducation().getId());
           user.setWorkexperienceId(user.getWorkExperience().getId());
           user.setPositionId(user.getPosition().getId());
           user.setEntryTimeId(user.getEntryTime().getId());
           user.setExpectedSalaryId(user.getExpectedSalary().getId());
            userinfoService.userdate(user);
        return AjaxResult.success("修改成功");
        }

        @GetMapping("nwlist")
        public AjaxResult nwlist(){
        List<NatureWork>natureWorkList=userinfoService.nwlistAll();
        return AjaxResult.success("查询成功").put("nwlist",natureWorkList);
        }

         @GetMapping("etlist")
         public AjaxResult etlist(){
         List<EntryTime> entryTimes = userinfoService.etlistAll();
        return AjaxResult.success("查询成功").put("etlist",entryTimes);
    }
    @GetMapping("eslist")
    public AjaxResult eslist(){
        List<ExpectedSalary> eslistAll = userinfoService.eslistAll();
        return AjaxResult.success("查询成功").put("eslist",eslistAll);
    }
    @GetMapping("ptlist")
    public AjaxResult ptlist(){
        List<Position> ptlistAll = userinfoService.ptlistAll();
        return AjaxResult.success("查询成功").put("ptlist",ptlistAll);
    }

    @PostMapping("updatajob")
    public AjaxResult updata(@RequestBody JobExperience jobExperienceForm ,Integer userinfoId){
        jobExperienceForm.setUserinfoId(userinfoId);
        if(jobExperienceForm.getId()==null){
            userinfoService.Addjob(jobExperienceForm);

        }else {
            userinfoService.updatajob(jobExperienceForm);
        }
        return AjaxResult.success("修改成功");
    }
    @PostMapping("delete")
    public AjaxResult delete(@RequestBody JobExperience jobExperience){
        userinfoService.delete(jobExperience);
        return AjaxResult.success("删除成功");
    }
    @GetMapping("listjob")
    public AjaxResult listjob( Integer id){
        List<JobExperience> jobExperienceList=userinfoService.Listjob(id);
        return AjaxResult.success("查询成功").put("listjob",jobExperienceList);
    }
    @PostMapping("UserListAll")
    public  AjaxResult listall(Integer id){
       Userinfo userinfo1= userinfoService.ListAll(id);
       return AjaxResult.success("查询成功").put("list",userinfo1);
    }
    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody  ProjectExperience projectExperienceForm,Integer userinfoId){
        projectExperienceForm.setUserinfoId(userinfoId);
        if(projectExperienceForm.getId()==null){
        userinfoService.addpe(projectExperienceForm);
        }else {
            userinfoService.saveOrUpdate(projectExperienceForm);
        }
        return AjaxResult.success("修改成功");
    }
    @PostMapping("aaa")
    public  AjaxResult aa(@RequestBody Userinfo resumeForm){
        userinfoMapper.updateById(resumeForm);
       Userinfo userinfo= userinfoMapper.selectById(resumeForm.getId());
        return AjaxResult.success("添加成功").put("userinfo",userinfo);

    }
    @GetMapping("edlist")
    public AjaxResult edlist(Integer id){
       List<EducationalExperience> experienceList= userinfoService.edlist(id);
       return AjaxResult.success("查询成功").put("edlist",experienceList);
    }
    @PostMapping("edsaveOrUpdate")
    public  AjaxResult edsaveOrUpdate(@RequestBody EducationalExperience educationForm,Integer userinfoId){
        educationForm.setUserinfoId(userinfoId);
        if(educationForm.getId()==null){
            userinfoService.inserted(educationForm);
        }else {
            userinfoService.edsaveOrUpdate(educationForm);
        }
        return AjaxResult.success("修改成功");
    }
    @GetMapping("deleteed")
    public AjaxResult deleteed(Integer id){
        userinfoService.deleteed(id);
        return AjaxResult.success("删除成功");
    }
    @GetMapping("deletepe")
    public  AjaxResult deletepe(Integer id){
        userinfoService.deletepe(id);
        return AjaxResult.success("删除成功");
    }

}
